package com.bdc.whatwhat.views.cell;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.adapters.NotificationAdapter;
import com.bdc.whatwhat.modele.Notification;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.utils.transform.CircleTransform;
import com.bdc.whatwhat.utils.transform.CropTransformation;
import com.squareup.picasso.Picasso;

/**
 * Created by kevin on 21/06/14.
 */
public class DemandeWhatWhatCell extends ViewCell<Whatter> implements View.OnClickListener {

    public interface DemandeDelegate {
        public void demander(int position);
    }


    SessionManager session;
    ImageView image;
    TextView prenom;
    TextView nom;
    TextView status;
    View demande;
    View reload;

    DemandeDelegate demandeDelegate;
    FaireWhatWhatTask.FaireWhatWhatTaskDelegate faireWhatWhatTaskDelegate;


    @Override
    public void animer() {

    }

    @Override
    public void construire() {
        if (getParams() != null && getParams().length >= 1) {
            Object param0 = getParams()[0];
            if (param0 instanceof DemandeDelegate)
                demandeDelegate = (DemandeDelegate) param0;
            if (param0 instanceof FaireWhatWhatTask.FaireWhatWhatTaskDelegate)
                faireWhatWhatTaskDelegate = (FaireWhatWhatTask.FaireWhatWhatTaskDelegate) param0;
        }
    }

    @Override
    public void charger() {
        this.image = (ImageView) findViewById(R.id.imageWatter);
        this.prenom = (TextView) findViewById(R.id.prenom);
        this.nom = (TextView) findViewById(R.id.nom);
        this.status = (TextView) findViewById(R.id.status);

        demande = findViewById(R.id.demande);
        reload = findViewById(R.id.reload);

        session = new SessionManager(getContext());
    }

    @Override
    public void remplir() {

        if (getObject() != null) {


            if (getObject().getPrenom() != null)
                this.prenom.setText(getObject().getPrenom());

            if (getObject().getNom() != null)
                this.nom.setText(getObject().getNom());

            if (getObject().getWhatWhat() != null && getObject().getWhatWhat().getTexte() != null)
                this.status.setText("\"" + getObject().getWhatWhat().getTexte() + " \"");
            else
                this.status.setText("Ne fais rien");

            if (getObject().getUrlImage() != null && !getObject().getUrlImage().trim().equalsIgnoreCase("")) {
                Picasso.with(context).load(getObject().getUrlImage()).transform(new CropTransformation(300)).transform(new CircleTransform()).into(image);
            } else {
                Log.e("Picasso", "error");
            }

        }

    }

    @Override
    public void ajouterListeners() {
        getView().setOnClickListener(this);
        findViewById(R.id.cell_content).setOnClickListener(this);
        findViewById(R.id.reload).setOnClickListener(this);
        findViewById(R.id.demande).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.reload:
                if (faireWhatWhatTaskDelegate != null) {
                    Log.e("delegate","pas null");

                    faireWhatWhatTaskDelegate.faire(session.getProfil().getWhatWhat());
                } else
                    Log.e("delegate", "null");
                break;
        }

    }
}
